package cn.hdu.liang.observer.weatherobservable;

import java.util.Observable;
import java.util.Observer;

public class ForecastDisplay implements Observer, DisplayElement {

    private double lastPressure;
    private double currentPressure = 29.93F;
    private Observable observable;

    public ForecastDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        System.out.print("Forecast: ");
        if(currentPressure > lastPressure) {
            System.out.println("气压在升高!");
        } else if(currentPressure < lastPressure) {
            System.out.println("气压在降低");
        } else {
            System.out.println("一切正常");
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof WeatherData) {
            WeatherData wd = (WeatherData) o;
            lastPressure = currentPressure;
            currentPressure = wd.getPressure();
            display();
        }
    }
}
